package com.xxg.renrenmessage.controller;

import com.xxg.renrencommon.domain.bean.MessageInfo;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenmessage.domain.query.ListMessageQuery;
import com.xxg.renrenmessage.domain.query.SetAllReadQuery;
import com.xxg.renrenmessage.domain.query.SetReadQuery;
import com.xxg.renrenmessage.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 消息管理接口
 *
 * @author szj
 * @date 2021/11/29
 */
@Api(tags = "消息管理")
@RestController
@RequestMapping("/message")
@AllArgsConstructor
public class MessageController {
    private final MessageService messageService;

    @ApiOperation("查询消息列表")
    @RequestMapping(value = "list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询消息列表参数", required = true, dataType = "ListMessageQuery")})
    public BaseResult<List<MessageInfo>> list(@RequestBody @Validated ListMessageQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return messageService.list(userId, query);
    }

    @ApiOperation("全部已读")
    @RequestMapping(value = "setAllRead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "全部已读参数", required = true, dataType = "SetAllReadQuery")})
    public BaseResult<Void> setAllRead(@RequestBody @Validated SetAllReadQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return messageService.setAllRead(userId, query);
    }

    @ApiOperation("将指定消息设为已读")
    @RequestMapping(value = "setRead", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "将指定消息设为已读参数", required = true, dataType = "SetReadQuery")})
    public BaseResult<Void> setRead(@RequestBody @Validated SetReadQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return messageService.setRead(userId, query);
    }

    @ApiOperation("将指定消息删除")
    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "将指定消息设为已读参数", required = true, dataType = "SetReadQuery")})
    public BaseResult<Void> delete(@RequestBody @Validated SetReadQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return messageService.delete(userId, query);
    }
}
